package com.bmf.file.utils;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.io.FileUtil;
import com.bmf.core.constant.ResultCodeEnum;
import com.bmf.core.exception.Assert;
import com.bmf.file.LoadPathConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 下载文件工具
 *
 * @author
 * @date 2022/10/07
 */
@Slf4j
public class DownloadFileUtil {
    private DownloadFileUtil() {
        throw new IllegalStateException("Utility class");
    }
    public static void downloadLocal(String url ,HttpServletResponse  response) {

        File file = new File(url);
        Assert.isTrue(file.exists(),"文件不存在");
        String name = file.getName();

        Path path = Paths.get(url);

        // 获取路径的第一级
        String firstLevel = path.getName(0).toString();
        Assert.isTrue("plugin".equals(firstLevel)||"data".equals(firstLevel)||"lib".equals(firstLevel), "输入地址有误");

        response.reset();
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Expose-Headers", "*");
        response.setHeader("Access-Control-Request-Headers", "*");
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);

        try (FileInputStream fileInputStream = new FileInputStream(file);
             BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
             ServletOutputStream responseOutputStream = response.getOutputStream();
             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(responseOutputStream)) {

            String fileName =  URLEncoder.encode(name , "UTF-8");
            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);

            byte[] buffer  = new byte[1024];
            int readLen;
            while ((readLen = bufferedInputStream.read(buffer)) !=-1) {
                bufferedOutputStream.write(buffer , 0, readLen);
            }
        } catch (Exception e) {
            log.error(ExceptionUtil.stacktraceToString(e));
            ResultCodeEnum.DOWNLOAD_ERROR.assertFail();
        }

    }
}
